package com.person.jc.service.buy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.Constants;
import com.person.jc.common.SQLSpell;
import com.person.jc.service.Service;
import com.person.jc.service.service.BsOrderService;
import com.person.jc.utils.Utils;

public class BsPartsConfirmService extends Service {
	
	public final static BsPartsConfirmService me = new BsPartsConfirmService();
	
	public List<Record> getList(Map<String, Object> map){
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT\n" );
		sql.append("	rl.order_id,\n" );
		sql.append("	cl.opt_user service_user,\n" );
		sql.append("	rl.cust_id,\n" );
		sql.append("	ta.leader_id,\n" );
		sql.append("	count(rl.opt_user) num1,\n" );
		sql.append("	sum(IFNULL(pc.zfNum, 0)) zfNum,\n" );
		sql.append("	sum(IFNULL(pc.wxNum, 0)) wxNum,\n" );
		sql.append("	("+SQLSpell.userName("cl.opt_user")+") service_user_,\n" );
		sql.append("	("+SQLSpell.customerName("rl.cust_id")+") cust_id_,\n" );
		sql.append("	("+SQLSpell.userName("ta.leader_id")+") leader_id_\n" );
		sql.append("FROM bs_repair_confirm_log cl\n" );
		sql.append("LEFT JOIN bs_repair_reg_log rl ON cl.log_id = rl.log_id\n" );
		sql.append("left join bs_task_assign ta on rl.order_id = ta.order_id and ta.order_type = 2\n" );
		sql.append("LEFT JOIN (SELECT t.log_id,SUM(IF(t.confirm_status = 0, 1, 0)) zfNum, SUM(IF(t.confirm_status = 1, 1, 0)) wxNum FROM bs_parts_confirm_log t GROUP BY t.log_id) pc ON pc.log_id = rl.log_id\n" );
		sql.append("LEFT JOIN bs_order o on rl.order_id = o.order_id\n" );
		sql.append("WHERE\n" );
		sql.append("	cl.confirm_status = 1\n" );
		sql.append("	AND o.flow_id = "+Constants.FLOW_WX_WXQR+"\n" );
		String temp = "";
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("custId")))){
			sql.append("and rl.cust_id  = ?\n" );
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("orderId")))){
			sql.append("and rl.cust_id  like ?\n" );
			params.add("%"+temp+"%");
		}
		sql.append("GROUP BY rl.order_id\n" );
		return Db.find(sql.toString(), Utils.getObjParam(params)); 
	}
	

	public List<Record> getViewList(Map<String, Object> map) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT\n");
		sql.append("	oe.*,rl.opt_user, ("+SQLSpell.userName("rl.opt_user")+") opt_user_,("+SQLSpell.kindName("volume_category", "oe.volume_category")+") volume_category_, rd.*,(SELECT pc.confirm_status from bs_parts_confirm_log pc where pc.log_id = rl.log_id) pc_confirm,\n");
		sql.append("	(select kind_name from ts_code_kind_data where kind_code = oe.device_type and type_code = 'device_type') device_type_,\n");
		sql.append("	(SELECT group_concat(t.device_name) from bd_device_info t where FIND_IN_SET(t.device_id, oe.gas_type)) gas_type_\n");
		sql.append("FROM bs_repair_confirm_log cl\n");
		sql.append("LEFT JOIN bs_repair_reg_log rl ON cl.log_id = rl.log_id\n");
		sql.append("left join bs_order_equipment oe on  rl.equ_id = oe.equ_id\n");
		sql.append("left join\n");
		sql.append("(select d.log_id, group_concat(distinct f.fault_name order by f.fault_name asc separator '; ') fault_name,group_concat(DISTINCT concat(p.parts_name, '[' , p.parts_code, ']') ORDER BY p.parts_name ASC, p.parts_code ASC separator '; ') parts_name  from bs_repair_check_detail d left join bd_fault_info f on d.fault_id = f.fault_id left join bd_device_parts p on d.parts_id = p.parts_id group by d.log_id ) rd on rd.log_id = rl.log_id\n");
		sql.append("WHERE cl.confirm_status = 1\n");
		String temp = "";
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("custId")))){
			sql.append("and rl.cust_id  = ?\n" );
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("orderId")))){
			sql.append("AND rl.order_id = ?\n");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("optUser")))){
			sql.append("AND rl.opt_user = ?\n");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("serviceId")))){
			sql.append("AND cl.opt_user = ?");
			params.add(temp);
		}
		return Db.find(sql.toString(), Utils.getObjParam(params)); 
	}
	
	public void save(String logIds, Object[][] params, int length){
		String sql = "INSERT INTO bs_parts_confirm_log (log_id, confirm_status, opt_user, opt_date) VALUES (?, ?, ?, now())";
		Db.batch(sql, params, length);
		super.setCRUD("配件确认", "C", "新增配件确认：" + logIds);
	}
	

	public void delete(String[] logId) {
		String temp = "";
		if(logId!=null && logId.length>0){
			for (int i = 0; i < logId.length; i++) {
				if(temp!=""){
					temp += ",";
				}
				temp += logId[i];
			}
		}
		String sql = "delete from bs_parts_confirm_log where log_id IN ("+temp+")";
		Db.update(sql);
	}
	
	//维修确认，变更订单FLOW
	public void changeOrderFlow(String orderId) {
		String sql = "select count(oe.equ_id) from bs_order_equipment oe \n" +
				"left join bs_repair_reg_log rl on oe.equ_id = rl.equ_id\n" +
				"left join bs_repair_confirm_log rcl on rl.log_id = rcl.log_id\n" +
				"left join bs_parts_confirm_log pcl on rl.log_id = pcl.log_id\n" +
				"where oe.order_id = ? \n" +
				"and rcl.confirm_status = 1\n" +
				"and pcl.log_id is null";
		Long num = Db.queryLong(sql, orderId);
		if(num.intValue() == 0){
			BsOrderService.me.updateFlow(orderId, Constants.FLOW_WX_PJQR);
		}
	}

}
